package com.busionline.wotu.woochenandroid.adapter;

import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.busionline.wotu.baselibrary.adapter.CommonRecyclerAdapter;
import com.busionline.wotu.woochenandroid.R;
import com.busionline.wotu.woochenandroid.bean.ArticleBean;
import com.busionline.wotu.woochenandroid.listener.OnArticleListItemClickListener;
import com.busionline.wotu.woochenandroid.tools.StringUtil;
import com.busionline.wotu.woochenandroid.tools.TimeUtil;

import java.util.List;

/**
 *首页列表数据
 *@author woochen123
 *@time 2018/5/15 18:02
 *@desc
 */

public class HomeAdapter extends CommonRecyclerAdapter<ArticleBean> {
    private OnArticleListItemClickListener listener;

    public void setOnArticleListItemClickListener(OnArticleListItemClickListener onArticleListItemClickListener) {
        this.listener = onArticleListItemClickListener;
    }

    public HomeAdapter(Context mContext, List<ArticleBean> mDatas, int mLayoutId) {
        super(mContext, mDatas, R.layout.item_home_article_list);
    }

    @Override
    public void convert(final ViewHolder holder, final ArticleBean item) {
        TextView tv_author = holder.getView(R.id.tv_author);
        final TextView tv_title = holder.getView(R.id.tv_title);
        TextView tv_time = holder.getView(R.id.tv_time);
        TextView tv_type = holder.getView(R.id.tv_type);
        ImageView img_collect = holder.getView(R.id.img_collect);
        tv_author.setText("作者: ");
        tv_author.append(StringUtil.getSpanText(item.getAuthor()));
        tv_time.setText(TimeUtil.parseTime(item.getPublishTime()));
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            //24以后废弃了Html.fromHtml(String)
            tv_title.setText(Html.fromHtml(item.getTitle(),Html.FROM_HTML_MODE_LEGACY));
        } else {
            tv_title.setText(Html.fromHtml(item.getTitle()));
        }
        tv_type.setText("分类: ");
        tv_type.append(StringUtil.getSpanText(item.getChapterName()));
        img_collect.setImageResource(item.isCollect() ? R.drawable.ic_favorite_light_24dp : R.drawable.ic_favorite_gray_24dp);
        tv_type.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onTreeClick(item.getChapterId(), item.getChapterName());
                }
            }
        });
        img_collect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onCollectClick(holder.getAdapterPosition(), item.getId());
                }
            }
        });
    }
}
